import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

if __name__=="__main__":
    f=r"D:\Tetuan City power consumption.csv"
    data=pd.read_csv(f)
    # print(data.head())
    # print(data)
    data.columns=['DateTime','Temperature','Humidity','x4','x5','x6','y1','y2','y3']
    # print(data)
    #第一列是递增的日期时间看作是索引不予考虑
    x=sm.add_constant(data.iloc[:,1])#生成自变量
    # print(x)
    y=data['Humidity']#生成因变量
    # print(y)
    model=sm.OLS(y,x)#生成模型
    result=model.fit()#模型拟合
    print(result.summary())#模型描述
    temp, ax = plt.subplots(figsize=(40,40))#调节像素4000*4000
    ax.plot(data['Temperature'], data['Humidity'], 'o', label='data')#将散点画入像素图中
    y_fitted=result.fittedvalues#通过模型计算的y
    ax.plot(data['Temperature'], y_fitted, 'r--.', label='OLS')#将拟合的回归函数画入图中
    plt.show()
